<template>
  <div class="sidebar">
    <div class="sidebar-header">
      <h3>上海大学数字人系统</h3>
    </div>
    
    <el-menu
      :default-active="activeMenu"
      class="sidebar-menu"
      @select="handleMenuSelect"
    >
      <!-- <el-menu-item index="dashboard">
        <el-icon><House /></el-icon>
        <span>仪表板</span>
      </el-menu-item> -->
      
      <!-- <el-menu-item index="tts">
        <el-icon><Microphone /></el-icon>
        <span>TTS语音合成</span>
      </el-menu-item>
      
      <el-menu-item index="musetalk">
        <el-icon><VideoCamera /></el-icon>
        <span>MuseTalk数字人</span>
      </el-menu-item> -->

      <el-menu-item index="digital">
        <el-icon><VideoCamera /></el-icon>
        <span>数字人生成</span>
      </el-menu-item>

      <el-menu-item index="history">
        <el-icon><House /></el-icon>
        <span>历史记录</span>
      </el-menu-item>
    </el-menu>
    
    <div class="sidebar-footer">
      <el-divider />
      <div class="user-info">
        <el-icon><User /></el-icon>
        <span>{{ userStore.userInfo.username }}</span>
      </div>
      <el-button
        type="danger"
        size="small"
        plain
        @click="handleLogout"
        style="width: 100%; margin-top: 10px;"
      >
        <el-icon><SwitchButton /></el-icon>
        退出登录
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessageBox } from 'element-plus'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const activeMenu = computed(() => {
  return route.name?.toLowerCase() || 'dashboard'
})

const handleMenuSelect = (index) => {
  const routeMap = {
    dashboard: '/dashboard',
    tts: '/tts',
    musetalk: '/musetalk',
    digital: '/digital-human',
    history: '/history'
  }
  
  if (routeMap[index]) {
    router.push(routeMap[index])
  }
}

const handleLogout = async () => {
  try {
    await ElMessageBox.confirm('确定要退出登录吗？', '退出确认', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    router.replace('/login')
    // console.log('用户确认退出')  // ✅ 调试
    // userStore.logout()
    // console.log('已调用 logout')  // ✅ 调试
    // router.push('/login')
    // console.log('正在跳转 /login')  // ✅ 调试
    // userStore.logout()
    // // 等 DOM 状态刷新后再跳转更安全
    // nextTick(() => {
    //   router.push('/login')
    // })
  } catch {
    // 用户取消退出
  }
}

</script>

<style scoped>
.sidebar {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.sidebar-header {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid var(--border-light);
}

.sidebar-header h3 {
  margin: 0;
  color: var(--text-primary);
  font-size: 18px;
  font-weight: 600;
}

.sidebar-menu {
  flex: 1;
  border-right: none;
}

.sidebar-footer {
  padding: 20px;
  border-top: 1px solid var(--border-light);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-regular);
  font-size: 14px;
  margin-bottom: 10px;
}

:deep(.el-menu-item) {
  height: 50px;
  line-height: 50px;
  margin: 4px 12px;
  border-radius: 8px;
}

:deep(.el-menu-item:hover) {
  background-color: var(--border-extra-light);
}

:deep(.el-menu-item.is-active) {
  background-color: var(--primary-color);
  color: white;
}

:deep(.el-menu-item.is-active .el-icon) {
  color: white;
}

:deep(.el-divider) {
  margin: 10px 0;
}
</style>
